<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 
        解题思路：
        - 构建一个表示状态的图
        - 遍历字符串，并沿着图走，如果到了某个节点无路可走就返回false
        - 遍历结束，如果走到3/5/6，就返回true，否则返回false
     -->
</head>
<body>
    <script>
        // 这个题用了一个编译原理中类似自动机的图
        var isNumber = function(s) {
            const graph = {
                // blank空格，sign加减号，digit数字
                0: {'blank':0, 'sign': 1, '.': 2, 'digit': 6},
                1: {'digit': 6, '.': 2},
                2: {'digit': 3},
                3: {'digit': 3, 'e': 4, 'E': 4},
                4: {'digit': 5, 'sign': 7},
                5: {'digit': 5},
                6: {'digit': 6, '.': 3, 'e': 4, 'E': 4},
                7: {'digit': 5}
            };

            let state = 0;
            for (c of s.trim()) {
                if(c >= 0 && c <= 9) {
                    c = 'digit';
                } else if (c === ' ') {
                    c = 'blank';
                } else if (c === '+' || c === '-') {
                    c = 'sign';
                }
                state = graph[state][c];
                if (state === undefined) {
                    return false;
                }
            }
            if (state == 3 || state == 5 || state == 6) {
                return true;
            }
            return false;
        };
    </script>
</body>
</html>